﻿using System;
using System.Configuration;


namespace BoardAppUtil.Formatting
{
    public static class Formatter
    {
        /// Author: Suren Manawatta.
        /// //Date: 2012-05-22.
        /// <summary>
        /// This function will accept a DateTime? variable and returns a formatted string (only the date part).
        /// Formatter is defined in Web.config file's appSettings section. key = "dateOnlyFormat".
        /// </summary>
        /// <param name="para">DateTime variable which need to be format.</param>
        /// <returns>String formatted date will be returned.</returns>
        public static string GetDateOnlyFormat(DateTime? para)
        {
            try
            {
                if (para == null)
                {
                    return "";
                }

                string format = ConfigurationManager.AppSettings["dateOnlyFormat"];
                return ((DateTime)para).ToString(format);
            }
            catch (Exception)
            {
                return para.ToString();
            }
        }

        /// Author: Suren Manawatta.
        /// Date: 2012-05-22.
        /// <summary>
        /// This function will accept a DateTime? variable and returns a formatted string.
        /// Formatter is defined in Web.config file's appSettings section. key = "dateTimeFormat".
        /// </summary>
        /// <param name="para">DateTime? variable which need to be format.</param>
        /// <returns>String formatted date and time will be returned.</returns>
        public static string GetDateTimeFormat(DateTime? para)
        {
            try
            {
                if (para == null)
                {
                    return "";
                }
                string format = ConfigurationManager.AppSettings["dateTimeFormat"];
                return ((DateTime)para).ToString(format);
            }
            catch (Exception)
            {
                return para.ToString();
            }
        }

        /// Author: Suren Manawatta.
        /// Date: 2012-05-23.
        /// <summary>
        /// This function returns the jquery date format according to the c#
        /// format used on the system.
        /// If something went wrong as default, "dd/MM/yyyy" format will return.
        /// </summary>
        /// <returns>String date format.</returns>
        public static string GetJqueryDateFormatOnly()
        {
            try
            {
                string format = ConfigurationManager.AppSettings["dateOnlyFormat"];
                string dateFormat = "";

                switch (format)
                {
                    #region Using "d" (Special Case)
                    case "d":
                        dateFormat = "m/dd/yy";
                        return dateFormat;
                    #endregion

                    #region Using "/"
                    case "dd/MMM/yy":
                        dateFormat = "dd/M/y";
                        return dateFormat;

                    case "M/dd/yyyy":
                        dateFormat = "m/dd/yy";
                        return dateFormat;

                    case "M/d/yy":
                        dateFormat = "m/d/y";
                        return dateFormat;

                    case "MM/dd/yy":
                        dateFormat = "mm/dd/y";
                        return dateFormat;

                    case "MM/dd/yyyy":
                        dateFormat = "mm/dd/yy";
                        return dateFormat;

                    case "yy/MM/dd":
                        dateFormat = "y/mm/dd";
                        return dateFormat;

                    case "yyyy/MM/dd":
                        dateFormat = "yy/mm/dd";
                        return dateFormat;
                    #endregion

                    #region Using "-"
                    case "dd-MMM-yy":
                        dateFormat = "dd-M-y";
                        return dateFormat;

                    case "M-dd-yyyy":
                        dateFormat = "m-dd-yy";
                        return dateFormat;

                    case "M-d-yy":
                        dateFormat = "m-d-y";
                        return dateFormat;

                    case "MM-dd-yy":
                        dateFormat = "mm-dd-y";
                        return dateFormat;

                    case "MM-dd-yyyy":
                        dateFormat = "mm-dd-yy";
                        return dateFormat;

                    case "yy-MM-dd":
                        dateFormat = "y-mm-dd";
                        return dateFormat;

                    case "yyyy-MM-dd":
                        dateFormat = "yy-mm-dd";
                        return dateFormat;
                    #endregion
                }

                //this is a rare case, will not execute most of the time
                //if swithch fails this format will return

                dateFormat = "mm/dd/y";
                return dateFormat;
            }
            catch (Exception)
            {
                //if something went wrong this will be used as
                //a default format of the date
                return "mm/dd/y";  //MM/DD/YY
            }
        }
    }
}
